﻿#ifndef SVR_COMMON_ASIO_ASIO_S2S_SESSION_HEAD__
#define SVR_COMMON_ASIO_ASIO_S2S_SESSION_HEAD__

#include <memory>  // for enable_shared_from_this<>

#include <boost/asio/spawn.hpp>  // for yield_context
#include <boost/asio/ip/tcp.hpp>  // for socket

#include "asio_session_base.h"  // for CAsioSessionBase

class CMailBox;
class CServerMailBox;

// Server to server session.
// Server connect to other server.
// The remote server will accept this connect and create a CAsionSvrSession.
// The remote server will also create a CAsioS2sSession and connect this server.
// This server will also accept connection from remote server and create a CAsioSvrSession.
// There are 2 connections and 4 sessions between 2 servers.
class CAsioS2sSession :
    public std::enable_shared_from_this<CAsioS2sSession>,
    public CAsioSessionBase
{
public:
    CAsioS2sSession(CAsioServer& rServer, CServerMailBox& rMailBox);
    virtual ~CAsioS2sSession();
public:
    void Go();

private:
    void LoopRead(boost::asio::yield_context yield);
    void LoopWrite(boost::asio::yield_context yield);
private:
    void Reconnect(boost::asio::yield_context yield);
    void WaitASec(boost::asio::yield_context yield);
    void Disconnect();

private:
    CMailBox& GetMailBox() override;
    const CMailBox& GetMailBox() const override;

private:
    CServerMailBox& m_rSvrMb;
    std::unique_ptr<socket> m_pSocket;
};  // class CAsioS2sSession

#endif  // SVR_COMMON_ASIO_ASIO_S2S_SESSION_HEAD__

